require 'sqlite3'

class Squick

  def lastSeen(msg)
    return nil unless(msg.content =~ /!seen ([\w]*)/)
    getNameList() #Request a Name List()
    msg = @socket.gets
    handleNameList(msg)

    begin
      date = @db.get_first_value("select timeStamp from lastSeen where nick=='#{$~[1]}'")
      if (date == nil) then
       return "I've never seen '#{$~[1]}' here.  Are you sure you typed it right?"
      end
       return "#{$~[1]} was last here at: #{date}"
    rescue
       return "I've never seen '#{$~[1]}' here.  Are you sure you typed it right?"
    end
  end

  def updateTimeStampFromMsg(msg)
    updateTimeStamp(msg.user)
  end

  def updateTimeStampsForList(names)
    names.each do |nick|
      updateTimeStamp(nick)
    end

    return nil  #Don't say anything in the channel
  end

  def updateTimeStamp(nick)
    if(nick =~ /^(@|\+).*/) then nick = nick.reverse.chop.reverse end #stip off any leading @/+ for ops
    begin
      @db.execute("delete from lastSeen where nick='#{nick}'")
    rescue
    end
    @db.execute("insert into lastSeen(nick, timeStamp) values('#{nick}', DATETIME('NOW'))")

    return nil #Don't say anything in the channel
  end

end
